import json


class Game_stats():
    def __init__(self, ai_settings):
        """初始化统计信息"""
        self.ai_settings = ai_settings
        self.score = 0
        # 在任何情况下都不应该重置最高得分
        self.high_score_file = ai_settings.high_score_file
        self.high_score = self.get_higtscore()
        self.reset_stats()
        # 让游戏一开始就处于非活动状态
        self.game_active = False
        self.game_over=False

    def reset_stats(self):
        """初始化值随游戏进行可能变化的统计信息"""
        # 飞船的数量
        self.ships_left = self.ai_settings.ship_limit
        self.score = 0
        self.level = 1

    def get_higtscore(self):
        """获取本地最高分的存储文件"""
        high_score = 0;

        try:
            with open(self.high_score_file) as file:
                high_score = json.load(file)
        except FileNotFoundError:
            with open(self.high_score_file, "w") as w_file:
                json.dump(0, w_file)
                print("检测到最高分储存文件不存在，正在创建。。。。")
        return int(high_score);

    def write_highsore(self):
        """写入最高分文件"""
        high_score = int(self.high_score)
        try:
            with open(self.high_score_file, "w") as w_file:
                json.dump(high_score, w_file)
        except Exception as ex:
            print("发生错误，case by %s" % ex)

        print("储存最高分成功，highscore=" + str(high_score))

